#include "examples.h"


void runmtLeastR(){
	int m,n,k,state,i;
	double *A, *xorin, *noise, *y, rho;
	OPTS opt;
	int ind[] = {0,100,200,300,400,500,600,700,800,900,1000};
	double c1,c2,q;
	stopWatch sw;
	FUNVAL result1,result2,result3;
	m = 1000;
	n = 100;
	k = 10;
	q=2.0;
	A= (double *) malloc( sizeof(double) * m* n);
	xorin = (double *) malloc(sizeof(double) * n*k);
	noise = (double *) malloc(sizeof(double) * m);
	y = (double *) malloc(sizeof(double) * m);
	
	state = 2344;
	acml_randseed(state);
	acml_randn(A,m*n,0.0,1.0);

	state = 13255;
	acml_randseed(state);
	acml_randn(xorin,n*k,0.0,1.0);

	state = 333;
	acml_randseed(state);
	acml_randn(noise,m,0.0,1.0);
	dcopy(m,noise,1,y,1);

	fprintM(C_TMP_DIR "A.mt.matrix",A,m,n);
	fprintM(C_TMP_DIR "xOrin.mt.matrix",xorin,n,k);
	fprintM(C_TMP_DIR "noise.mt.matrix",y,m,1);


	for(i=0; i<k; i++)
		dgemv('N',ind[i+1]-ind[i],n,1.0,&A[ind[i]],m,&xorin[n*i],1,0.01l,&y[ind[i]],1);
	//dgemv('N',m,n,1.0,A,m,xorin,1,0.01l,y,1);

	fprintM(C_TMP_DIR "A.mt.matrix",A,m,n);
	fprintM(C_TMP_DIR "y.mt.matrix",y,m,1);
	rho = 0.5l;
	memset(A,0,m*n*sizeof(double));
	memset(y,0,m*sizeof(double));
	fscanM(C_TMP_DIR "A.mt.matrix",A,m,n);
	fscanM(C_TMP_DIR "y.mt.matrix",y,m,1);

	c2 = 0.5;

	initOPTS(&opt,m,n,k);
	opt.sWeight[0] = 1.0;
	opt.sWeight[1] = 1.0;

	opt.q = q;
	setOPTind(&opt,ind,k+1);
	opt.rsL2 = 0;
	opt.init = 2;
	opt.maxIter = 100;
	opt.nFlag = 0;
	opt.rsL2 = 0;
	opt.tFlag = 5;
	opt.rFlag = 0;

	opt.mFlag=0;
	opt.lFlag =0;
	startTimer(&sw);
	result1 = mtLeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(C_TMP_DIR "mt.x1",result1.x,n,k);
	fprintM(C_TMP_DIR "mt.fVal1",result1.funVal,opt.maxIter,1);
	fprintM(C_TMP_DIR "mt.L1",result1.ValueL,opt.maxIter,1);
	free(result1.x);
	free(result1.funVal);
	free(result1.ValueL);

	opt.mFlag=1;
	opt.lFlag =0;
	startTimer(&sw);
	result2 = mtLeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(C_TMP_DIR "mt.x2",result2.x,n,k);
	fprintM(C_TMP_DIR "mt.fVal2",result2.funVal,opt.maxIter,1);
	fprintM(C_TMP_DIR "mt.L2",result2.ValueL,opt.maxIter,1);
	free(result2.x);
	free(result2.funVal);
	free(result2.ValueL);

	opt.mFlag=1;
	opt.lFlag =1;
	startTimer(&sw);
	result3 = mtLeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM(C_TMP_DIR "mt.x3",result3.x,n,k);
	fprintM(C_TMP_DIR "mt.fVal3",result3.funVal,opt.maxIter,1);
	fprintM(C_TMP_DIR "mt.L3",result3.ValueL,opt.maxIter,1);
	free(result3.x);
	free(result3.funVal);
	free(result3.ValueL);

}

